package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.HyrjMachine;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.HyrjMachineFaults;
import com.ruoyi.system.service.IHyrjMachineFaultsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 洗衣机故障管理，用于记录洗衣机的故障信息和维修情况Controller
 *
 * @author ruoyi
 * @date 2024-03-08
 */
@RestController
@RequestMapping("/system/faults")
public class HyrjMachineFaultsController extends BaseController {
    @Autowired
    private IHyrjMachineFaultsService hyrjMachineFaultsService;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private ISysRoleService iSysRoleService;

    /**
     * 查询洗衣机故障管理，用于记录洗衣机的故障信息和维修情况列表
     */
    @PreAuthorize("@ss.hasPermi('system:faults:list')")
    @GetMapping("/list")
    public TableDataInfo list(HyrjMachineFaults hyrjMachineFaults) {


        List<String> roleKeyList = new ArrayList<>();
        SysUser sysUser = iSysUserService.selectUserById(getUserId());
        if (null != sysUser) {
            List<Long> longs = iSysRoleService.selectRoleListByUserId(sysUser.getUserId());
            for (Long aLong : longs) {
                SysRole sysRole = iSysRoleService.selectRoleById(aLong);
                if (null != sysRole) {
                    roleKeyList.add(sysRole.getRoleKey());
                }
            }
        }
        if (!roleKeyList.contains("amdin") && !roleKeyList.contains("maintain")) {
            hyrjMachineFaults.setCreateBy(String.valueOf(getUserId()));
        }

        List<HyrjMachineFaults> list = hyrjMachineFaultsService.selectHyrjMachineFaultsList(hyrjMachineFaults);

        List<SysUser> sysUsers = iSysUserService.selectUserList(new SysUser());

        Map<Long, List<SysUser>> groupedById = sysUsers.stream()
                .collect(Collectors.groupingBy(SysUser::getUserId));


        startPage();


        for (HyrjMachineFaults machineFaults : list) {

            List<SysUser> sysUsers1 = groupedById.get(machineFaults.getOperatorId());
            if (null != sysUsers1) {
                machineFaults.setOperatorName(sysUsers1.get(0).getUserName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出洗衣机故障管理，用于记录洗衣机的故障信息和维修情况列表
     */
    @PreAuthorize("@ss.hasPermi('system:faults:export')")
    @Log(title = "洗衣机故障管理，用于记录洗衣机的故障信息和维修情况", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HyrjMachineFaults hyrjMachineFaults) {
        List<HyrjMachineFaults> list = hyrjMachineFaultsService.selectHyrjMachineFaultsList(hyrjMachineFaults);
        ExcelUtil<HyrjMachineFaults> util = new ExcelUtil<HyrjMachineFaults>(HyrjMachineFaults.class);
        util.exportExcel(response, list, "洗衣机故障管理，用于记录洗衣机的故障信息和维修情况数据");
    }

    /**
     * 获取洗衣机故障管理，用于记录洗衣机的故障信息和维修情况详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:faults:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(hyrjMachineFaultsService.selectHyrjMachineFaultsById(id));
    }

    /**
     * 新增洗衣机故障管理，用于记录洗衣机的故障信息和维修情况
     */
    @PreAuthorize("@ss.hasPermi('system:faults:add')")
    @Log(title = "洗衣机故障管理，用于记录洗衣机的故障信息和维修情况", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HyrjMachineFaults hyrjMachineFaults) {
        hyrjMachineFaults.setCreateBy(String.valueOf(getUserId()));
        return toAjax(hyrjMachineFaultsService.insertHyrjMachineFaults(hyrjMachineFaults));
    }

    /**
     * 修改洗衣机故障管理，用于记录洗衣机的故障信息和维修情况
     */
    @PreAuthorize("@ss.hasPermi('system:faults:edit')")
    @Log(title = "洗衣机故障管理，用于记录洗衣机的故障信息和维修情况", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HyrjMachineFaults hyrjMachineFaults) {

        return toAjax(hyrjMachineFaultsService.updateHyrjMachineFaults(hyrjMachineFaults));
    }

    /**
     * 删除洗衣机故障管理，用于记录洗衣机的故障信息和维修情况
     */
    @PreAuthorize("@ss.hasPermi('system:faults:remove')")
    @Log(title = "洗衣机故障管理，用于记录洗衣机的故障信息和维修情况", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(hyrjMachineFaultsService.deleteHyrjMachineFaultsByIds(ids));
    }


    /**
     * 修改【请填写功能名称】
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody HyrjMachineFaults hyrjMachineFaults) {
        hyrjMachineFaults.setOperatorId(getUserId());
        return toAjax(hyrjMachineFaultsService.updateHyrjMachine(hyrjMachineFaults));
    }


}
